package com.tony.pmp.server.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tony.pmp.common.response.BaseResponse;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.common.utils.Constant;
import com.tony.pmp.common.utils.ValidatorUtil;
import com.tony.pmp.model.entity.SysDeptEntity;
import com.tony.pmp.server.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysDepController
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/19/20:43
 * @Description: 部门接口
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController{

    @Autowired
    private SysDeptService sysDeptService;

    /**
    * @Description: list 获取部门列表
    * @Param: []
    * @return: java.util.List<com.tony.pmp.model.entity.SysDeptEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/19 21:18
    */
    @RequestMapping("/list")
    @RequiresPermissions(value = {"sys:dept:list"})
    public List<SysDeptEntity> list() {
        return sysDeptService.queryAll(Maps.newHashMap());
    }

    /**
    * @Description: info 获取顶级部门id
    * @Param: []
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/19 21:34
    */
    @RequestMapping("/info")
    public BaseResponse info() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        Long deptId = 0L;
        try {
            //数据视野决定顶级部门id可能不是0
            if (getUserId() != Constant.SUPER_ADMIN) {

            }
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        resMap.put("deptId",deptId);
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: select 获取部门树
    * @Param: []
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 9:32
    */
    @RequestMapping("/select")
    public BaseResponse select() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        List<SysDeptEntity> deptList = Lists.newLinkedList();
        try {
            deptList = sysDeptService.queryAll(Maps.newHashMap());
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        resMap.put("deptList",deptList);
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: save 新增部门信息
    * @Param: [entity, result]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 9:52
    */
    @RequestMapping(value = "/save",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:dept:save"})
    public BaseResponse save(@RequestBody @Validated SysDeptEntity entity, BindingResult result) {
        String res = ValidatorUtil.checkResult(result);
        entity.setDelFlag(0);
        if (StringUtils.isNotBlank(res)) {
            return new BaseResponse(StatusCode.Fail.getCode(),res);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("新增部门的信息为：{}",entity);
            log.info("新增部门是否成功：{}",sysDeptService.save(entity));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: detail 根据id获取部门详情
    * @Param: [deptId]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 10:14
    */
    @RequestMapping("/detail/{deptId}")
    @RequiresPermissions("sys:dept:info")
    public BaseResponse detail(@PathVariable Long deptId) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            resMap.put("dept",sysDeptService.getById(deptId));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: update 修改部门
    * @Param: [entity, result]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 13:21
    */
    @RequestMapping(value = "/update",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:dept:update"})
    public BaseResponse update(@RequestBody @Validated SysDeptEntity entity,BindingResult result) {
        String res = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)) {
            return new BaseResponse(StatusCode.Fail.getCode(),res);
        }
        if (entity.getDeptId() == null || entity.getDeptId() <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("修改部门信息为：{}",entity);
            log.info("是否修改成功：{}",sysDeptService.updateById(entity));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: delete 删除部门信息
    * @Param: [deptId]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 13:52
    */
    @RequestMapping("/delete")
    @RequiresPermissions(value = {"sys:dept:delete"})
    public BaseResponse delete(Long deptId) {
        if (deptId == null || deptId <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("删除部门的id为：{}",deptId);
            //如果当前部门有子部门，则需要要求先删除下面的所有部门再删除当前部门
            List<Long> subIds = sysDeptService.queryDeptIds(deptId);
            if (subIds != null && !subIds.isEmpty()) {
                return new BaseResponse(StatusCode.DeptHasSubDeptCanNotBeDelete);
            }
            //删除
            sysDeptService.removeById(deptId);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }
}
